package com.dongzili.demo.common.vo;

import lombok.Getter;
import lombok.Setter;

import java.io.Serializable;
import java.util.StringJoiner;

@Getter
@Setter
public class ApiResponse<T> implements Serializable {
    private static final long serialVersionUID = 1L;

    /**
     * 状态码
     */
    private Integer code;

    /**
     * 提示信息
     */
    private String message;

    /**
     * 状态
     */
    private String status;

    /**
     * 请求执行时间
     */
    private long times;

    /**
     * 数据封装
     */
    private T data;

    public ApiResponse(Integer code,String message, String status){
        this.code = code;
        this.message = message;
        this.status = status;
    }

    public ApiResponse(Integer code,String message, String status, T data){
        this(code,message,status);
        this.status = status;
        this.data = data;
    }

    public static <T> ApiResponse<T> success(){
        return new ApiResponse<>(ResponseCode.SUCCESS.getCode(),ResponseCode.SUCCESS.getMessage(), ResponseCode.SUCCESS.getStatus());
    }
    public static <T> ApiResponse<T> success(String message){
        return new ApiResponse<>(ResponseCode.SUCCESS.getCode(), message, ResponseCode.SUCCESS.getStatus());
    }

    public static <T> ApiResponse<T> success(T data){
        return new ApiResponse<>(ResponseCode.SUCCESS.getCode(),ResponseCode.SUCCESS.getMessage(), ResponseCode.SUCCESS.getStatus(),data);
    }

    public static <T> ApiResponse<T> success(T data,String message){
        return new ApiResponse<>(ResponseCode.SUCCESS.getCode(), message, ResponseCode.SUCCESS.getStatus(),data);
    }

    public static <T> ApiResponse<T> fail(){
        return new ApiResponse<>(ResponseCode.FAILURE.getCode(),ResponseCode.FAILURE.getMessage(), ResponseCode.FAILURE.getStatus());
    }

    public static <T> ApiResponse<T> fail(Integer code,String message,String status){
        return new ApiResponse<>(code,message, status);
    }

    public static <T> ApiResponse<T> error(){
        return new ApiResponse<>(ResponseCode.ERROR.getCode(),ResponseCode.ERROR.getMessage(), ResponseCode.ERROR.getStatus());
    }

    public static <T> ApiResponse<T> error(String msg){
        return new ApiResponse<>(ResponseCode.ERROR.getCode(), msg, ResponseCode.ERROR.getStatus());
    }

    public static <T> ApiResponse<T> error(String msg, T data){
        return new ApiResponse<>(ResponseCode.ERROR.getCode(), msg, ResponseCode.ERROR.getStatus(), data);
    }

    public static <T> ApiResponse<T> error(ResponseCode response){
        return new ApiResponse<>(response.getCode(), response.getMessage(), response.getStatus());
    }
//    public static <T> ApiResponse<T> error(ResponseCode response, String msg){
//        return new ApiResponse<>(response.getCode(), msg, response.getStatus());
//    }
    public static <T> ApiResponse<T> error(ResponseCode response, T data){
        return new ApiResponse<>(response.getCode(), response.getMessage(), response.getStatus(), data);
    }

    @Override
    public String toString() {
        return (new StringJoiner(", ", ApiResponse.class.getSimpleName() + "[", "]"))
                .add("code=" + this.code)
                .add("message='" + this.message + "'")
                .add("data=" + this.data)
                .toString();
    }
}
